package com.gambi.example.tapestry.components.gwtui;

import org.apache.tapestry5.Block;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.annotations.SupportsInformalParameters;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.gambi.example.client.GWTUI;
import com.gambi.example.client.gwtui.services.CheckBoxService;
import com.gambi.example.tapestry.components.GWTComponent;
import com.gambi.example.tapestry.pages.Start;

/*
 * TODO Define an AssetProviderService to provide the right library by looking at the project template folder, 
 * define and use a convention. NOTE: di js MUST MATCH the one provided in the class definition, otherwise the objects namespace
 * will not match at all during the onModuleLoad. This is a constraint to enforce some-how.
 */

@SupportsInformalParameters
@IncludeJavaScriptLibrary(value = { "context:js/gwtjs/com.gambi.example.GWTUI/com.gambi.example.GWTUI.nocache.js" })
public class CheckBox extends GWTComponent<GWTUI, CheckBoxService> {

	@Parameter(defaultPrefix = "literal")
	@Property
	private String div;

	@Parameter
	@Property
	private Object context;

	@Inject
	private ComponentResources componentResources;

	private Link link;

	@SetupRender
	void createEventLink() {
		link = componentResources.createEventLink("change",
				(context != null) ? new Object[] { context } : new Object[] {});
	}

	public String getEventLink() {
		return link.toAbsoluteURI();
	}

	public Class<GWTUI> getGWTModuleClass() {
		return GWTUI.class;
	}

	@Property
	private Boolean value;

	@Log
	@OnEvent("change")
	public Object method(Boolean value) {
		System.out.println("CheckBox.onChange() String value " + value);
		// return "The value is <b>" + value + "<b>";
		this.value = value;
		return Start.class;
	}
}
